/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ${basePackage}.${subPackage}.controller;


import ${basePackage}.${subPackage}.entity.${upperFirst(name)};
<?py if extend != "":?>
//此处导入本类的父类

<?py  properties = properties + models[extend]["properties"] ?>

<?py  if extend not in baseDataTypes: ?>

<?py    #print extend ?>

import ${basePackage}.${models[extend]["subPackage"]}.entity.${upperFirst(extend)};

<?py  #endif?>


<?py #endif?>

//此处导入本类引用的类
<?py for property in properties: ?>
<?py  if property["dataType"] not in baseDataTypes: ?>
<?py    datatype =  property["dataType"]?>
<?py    #print datatype ?>

import ${basePackage}.${models[datatype]["subPackage"]}.entity.${upperFirst(datatype)};

<?py  elif property["dataType"] == "List":?>
<?py    elementType =  property["elementType"]?>
import ${basePackage}.${models[elementType]["subPackage"]}.entity.${upperFirst(elementType)};


<?py  #endif?>
<?py #endfor ?>

import ${basePackage}.${subPackage}.service.*;

import com.bonc.common.bean.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * ${upperFirst(name)}的RestController
 * @author 自动生成的代码
 */
@RestController
@RequestMapping(value = "/api/${name}")
public class ${upperFirst(name)}RestController {
    
        
     @Autowired
    private ${upperFirst(name)}Service ${name}Service;


    

    /**
     * @return the ${name}Service
     */
    public ${upperFirst(name)}Service get${upperFirst(name)}Service() {
        return ${name}Service;
    }

    /**
     * @param ${name}Service the ${name}Service to set
     */
    public void set${upperFirst(name)}Service(${upperFirst(name)}Service ${name}Service) {
        this.${name}Service = ${name}Service;
    }

    /**
     * 新增${comment}
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResultMessage post( ${upperFirst(name)} ${name}) {


   
    
    <?py for property in properties: ?>
    <?py    if property["dataType"] in ["Set","List"]:?>
        //预处理集合${property["name"]}
        List ${property["name"]} = ${name}.get${upperFirst(property["name"])}();
        List<${upperFirst(property["elementType"])}> ${property["name"]}2 = new ArrayList<${upperFirst(property["elementType"])}>();
        ${upperFirst(property["elementType"])} ${property["elementType"]} = new ${upperFirst(property["elementType"])}();
        ${property["elementType"]}.setId(null);
        ${property["name"]}2.add(${property["elementType"]});
        if(${property["name"]} != null) {
            ${property["name"]}.removeAll(${property["name"]}2); 
            if (${property["name"]}.size() == 0) {
                ${name}.set${upperFirst(property["name"])}(null);
            }
        }
        
        

    <?py    elif property["dataType"]  not in baseDataTypes:?>

        if (${name}.get${upperFirst(property["name"])}().getId() == null || ${name}.get${upperFirst(property["name"])}().getId() == 0) {
             ${name}.set${upperFirst(property["name"])}(null);
        }

    <?py    #endif?>
    <?py #endfor ?>

       
         ${upperFirst(name)} obj = null;
         ResultMessage result = null;
         try{
            obj = get${upperFirst(name)}Service().create(${name});
            result = new ResultMessage(true,"新增成功!",obj);

         }catch(Exception ex){



            result = new ResultMessage(false,"新增失败!原因:"+ex.getLocalizedMessage(),obj);

         }
        
        

        return result;

    }

   
    /**
     * 提取单个${comment}
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ${upperFirst(name)} get(@PathVariable long id) {

        ${upperFirst(name)} ${name} = get${upperFirst(name)}Service().read(id);

        return ${name};

    }
    
    

    
    /**
     * 列出所有${comment}
     *
     * @return
     */
    @RequestMapping(value="/all",method = RequestMethod.GET)
    public List<${upperFirst(name)}> get() {
       
       
        return get${upperFirst(name)}Service().listAll();

    }
    /**
     * 还分页排序的多条件查询，
     * 
     * @return 
     */
    
    
     @RequestMapping(method = RequestMethod.GET)
    public Map get(${upperFirst(name)} ${name},PageContent pageContent) {

    <?py for property in properties: ?>
    <?py    if property["dataType"] not in baseDataTypes:?>
               if(${name}.get${upperFirst(property["name"])}()==null || (${name}.get${upperFirst(property["name"])}().getId() == null || ${name}.get${upperFirst(property["name"])}().getId()==0)){
                   ${name}.set${upperFirst(property["name"])}(null);
               }
    <?py    else: ?>
                if(${name}.get${upperFirst(property["name"])}() == null || ${name}.get${upperFirst(property["name"])}().equals("")){
                   ${name}.set${upperFirst(property["name"])}(null);
               }
    <?py    #endif?>
    <?py #endfor ?>   
        
        return get${upperFirst(name)}Service().findByCondition(${name},pageContent);

    }

    /**
     * 删除一条${comment}
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public ResultMessage delete(@PathVariable long id) {
        
       
 
        get${upperFirst(name)}Service().delete(id);

        return new ResultMessage(true, "删除成功",null);

    }

    /**
     * 更新一条${comment}
     *
     * @param id
     * @return
     */
   
    @RequestMapping(value="{id}",method = RequestMethod.PUT)
    public ResultMessage put(${upperFirst(name)} ${name}) {
        
    <?py for property in properties: ?>
    <?py    if property["dataType"] in ["Set","List"]:?>
        //预处理集合${property["name"]}
        List ${property["name"]} = ${name}.get${upperFirst(property["name"])}();
        List<${upperFirst(property["elementType"])}> ${property["name"]}2 = new ArrayList<${upperFirst(property["elementType"])}>();
        ${upperFirst(property["elementType"])} ${property["elementType"]} = new ${upperFirst(property["elementType"])}();
        ${property["elementType"]}.setId(null);
        ${property["name"]}2.add(${property["elementType"]});
        if(${property["name"]}!=null) {
            ${property["name"]}.removeAll(${property["name"]}2);  
            if (${property["name"]}.size() == 0) {
                ${name}.set${upperFirst(property["name"])}(null);
            }
        }
    <?py    elif property["dataType"]  not in baseDataTypes:?>

        if (${name}.get${upperFirst(property["name"])}().getId() == null || ${name}.get${upperFirst(property["name"])}().getId() == 0) {
             ${name}.set${upperFirst(property["name"])}(null);
        }
    
    <?py    #endif ?>   
    <?py #endfor ?>        

        get${upperFirst(name)}Service().update(${name});

        return new ResultMessage(true,"更新成功",${name});

    }
    
   
    /**
     *  注册日期类数据绑定，自动进行yyyy-MM-dd格式的字符串转换成java.util.Date
     * @param binder 
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));//true表示允许为空
    }

    
    
}
